package com.monkeybrother.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@RestController
public class ResourcesController {
    @Autowired
    private ResourceLoader resourceLoader;
    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("readFileFromJarWithResourceLoader")
    public void readFileFromJarWithResourceLoader() {
        try {
            Resource resource = resourceLoader.getResource("classpath:data.txt");
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            System.err.println("Error reading file from jar: " + e.getMessage());
        }
    }


    @GetMapping("readFileFromJarWithResourceAsStream")
    public void readFileFromJarWithResourceAsStream() {
        InputStream inputStream = getClass().getResourceAsStream("/data.txt");
        if (inputStream != null) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
                String line;
                StringBuilder contentBuilder = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    contentBuilder.append(line).append("\n");
                }
                System.out.println(contentBuilder.toString());
            } catch (IOException e) {
                System.err.println("An error occurred while reading the file: " + e.getMessage());
            }
        } else {
            System.out.println("File not found: application.yml");
        }
    }

    @GetMapping("readFileFromJarWithStreamUtils")
    public void readFileFromJarWithStreamUtils() {
        try {
            Resource resource = new ClassPathResource("data.txt");
            String s = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
            System.out.println(s);
        } catch (IOException e) {
            throw new RuntimeException("Failed to read file content", e);
        }
    }


    @GetMapping("readFileFromJarWithResourcePatternResolver")
    public void readFileFromJarWithResourcePatternResolver() {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            Resource resource = resolver.getResource("data.txt");
            if (resource.exists()) {
                String content = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
                System.out.println(content);
            } else {
                System.err.println("The resource 'data.txt' does not exist.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("readFileFromJarWithApplicationContext")
    public void readFileFromJarWithApplicationContext() {
        Resource resource = applicationContext.getResource("classpath:data.txt");
        try (InputStream inputStream = resource.getInputStream()) {
            String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
            System.out.println(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
